EnsurePythonVersion(2,4)
EnsureSConsVersion(1,2,0)

env = envd = None
variants = [
	{
		'name': 'release',
		'env': env,
		'suffix': '',
		'flags_win32': '-O2 -DNDEBUG -MD -arch:SSE2',
		'flags_posix': ''
	},
	{
		'name': 'debug',
		'env': envd,
		'suffix': 'd',
		'flags_win32': '-D_DEBUG -vmg -RTC1 -GF -MDd -Z7',
		'flags_posix': '-g'
	}
]

vars = Variables('scons.conf', ARGUMENTS)
for v in variants:
	vars.AddVariables(BoolVariable(v['name'], 'Set to ''1'' to build '+v['name'], True))
vars.AddVariables(
	PathVariable('OPENCV_ROOT', 'OpenCV root directory.', None, PathVariable.PathAccept),
	PathVariable('OPENFRAMEWORKS_ROOT', 'OpenFrameworks root directory.', None, PathVariable.PathAccept),
)

for v in variants:
	VariantDir('build/'+v['name'], '.', duplicate=0)
SConsignFile('build/.sconsign.dblite')
env = Environment(
	OS=str(ARGUMENTS.get('OS', Platform())),
	TARGET_ARCH = "x86", #"x86_64"
	variables=vars,
	tools = ['default'],
	CONFIGUREDIR='#build/.sconf_temp',
	CONFIGURELOG='#build/config.log',
	DSUF = ''
)
Help(vars.GenerateHelpText(env))

if (env['OS']=='win32'):
	t = Tool('msvc')
	t(env)
	#env['MSVS_VERSION'] = '8.0'
	print "Using msvs version ",env['MSVS_VERSION']
elif (env['OS']=='posix'):
	t = Tool('gcc')
	t(env)
else:
	print '*** error: platform',ARGUMENTS.get('OS',Platform()),'is not supported!'
	Exit(1)

if (env['OS']=='win32'):
	da = env.ParseFlags('-EHsc -nologo -D_MBCS -D"_WIN32_WINNT=0X0400" -GR -FD -D_CONSOLE -DPOCO_STATIC')
	env.MergeFlags(da)
elif (env['OS']=='posix'):
	da = env.ParseFlags('-fPIC -Wall -D_CONSOLE -DPOCO_STATIC')
	env.MergeFlags(da)

for v in variants:
	v['env'] = env.Clone()
	v['env']['DSUF'] = v['suffix']

for v in variants:
	if (env['OS']=='win32'):
		dt = v['env'].ParseFlags(v['flags_win32'])
		v['env'].MergeFlags(dt)
	elif (env['OS']=='posix'):
		dd = v['env'].ParseFlags(v['flags_posix'])
		v['env'].MergeFlags(dd)

v['env'].SConscript(['opencv.conf.py'])
Import('CheckOpenCV')
v['env'].SConscript(['openframeworks.conf.py'])
Import('CheckOpenFrameworks')

# Checks for libraries, header files, etc. go here!
for v in variants:
	if (env[v['name']]):
		conf = v['env'].Configure(custom_tests = { 'CheckOpenCV' : CheckOpenCV, 'CheckOpenFrameworks' : CheckOpenFrameworks } )
		# Check OpenCV
		if not conf.CheckOpenCV(v['env']['OPENCV_ROOT']):
			print
			print '*** error: Set $OPENCV_ROOT in ''scons.conf'' or edit opencv.conf.py!'
			Exit(1)
		# Check OpenFrameworks
		if not conf.CheckOpenFrameworks(v['env']['OPENFRAMEWORKS_ROOT']):
			print
			print '*** error: Set $OPENFRAMEWORKS_ROOT in ''scons.conf'' or edit openframeworks.conf.py!'
			Exit(1)
		v['env'] = conf.Finish()

if (env['OS']=='win32'):
	for v in variants:
		#manifest 
		v['env']['LINKCOM'] = [v['env']['LINKCOM'], 'mt.exe -nologo -manifest ${TARGET}.manifest -outputresource:$TARGET;1']
		v['env']['SHLINKCOM'] = [v['env']['SHLINKCOM'], 'mt.exe -nologo -manifest ${TARGET}.manifest -outputresource:$TARGET;2']
		v['env']['WINDOWS_INSERT_MANIFEST'] = True
		#env['WINDOWS_INSERT_DEF'] = True

for v in variants:
	if (env[v['name']]):
		v['env'].SConscript('build/'+v['name']+'/OpenFrameworks_lib/SConscript', exports={'env':v['env']}, duplicate=0)
